package com.getpebble.android.framework.protocol.outbound;

import android.text.TextUtils;
import com.getpebble.android.R;
import com.getpebble.android.common.framework.install.PutBytesType;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.util.ByteUtils;
import com.google.common.primitives.UnsignedInteger;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.Arrays;

public class PblOutboundPutBytesMessage extends PblOutboundMessage {
    private PutBytesCommand mCommand;
    private UnsignedInteger mCookie;
    private UnsignedInteger mCrc;
    private String mFilename;
    private int mIndex;
    private byte[] mPayload;
    private UnsignedInteger mSize;
    private PutBytesType mType;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundPutBytesMessage$PutBytesCommand = new int[PutBytesCommand.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundPutBytesMessage$PutBytesCommand[PutBytesCommand.INIT.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundPutBytesMessage$PutBytesCommand[PutBytesCommand.PUT.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundPutBytesMessage$PutBytesCommand[PutBytesCommand.COMMIT.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundPutBytesMessage$PutBytesCommand[PutBytesCommand.ABORT.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundPutBytesMessage$PutBytesCommand[PutBytesCommand.INSTALL.ordinal()] = 5;
            } catch (NoSuchFieldError e5) {
            }
        }
    }

    public enum PutBytesCommand {
        INIT((byte) 1),
        PUT((byte) 2),
        COMMIT((byte) 3),
        ABORT((byte) 4),
        INSTALL((byte) 5);
        
        private final byte mId;

        private PutBytesCommand(byte id) {
            this.mId = id;
        }

        public byte getId() {
            return this.mId;
        }
    }

    private PblOutboundPutBytesMessage(PutBytesCommand command, UnsignedInteger cookie) {
        super(EndpointId.PUT_BYTES);
        this.mCommand = command;
        this.mCookie = cookie;
    }

    private void setSize(UnsignedInteger size) {
        this.mSize = size;
    }

    private void setType(PutBytesType type) {
        this.mType = type;
    }

    private void setIndex(int index) {
        this.mIndex = index;
    }

    private void setFilename(String filename) {
        this.mFilename = filename;
    }

    private void setPayload(byte[] payload) {
        this.mPayload = payload;
    }

    private void setCrc(UnsignedInteger crc) {
        this.mCrc = crc;
    }

    public static PblOutboundPutBytesMessage createInit(UnsignedInteger size, PutBytesType type, int index, String filename) {
        PblOutboundPutBytesMessage message = new PblOutboundPutBytesMessage(PutBytesCommand.INIT, null);
        message.setSize(size);
        message.setType(type);
        message.setIndex(index);
        message.setFilename(filename);
        return message;
    }

    private void writeInitBytes() {
        addBytes(Byte.valueOf(this.mCommand.getId()));
        addBytes(ByteUtils.unsignedInt2bytes(this.mSize));
        addBytes(Byte.valueOf(this.mType.getId()));
        addBytes(Byte.valueOf((byte) this.mIndex));
        if (!TextUtils.isEmpty(this.mFilename)) {
            ByteUtils.writeNullTerminatedString(this, this.mFilename);
        }
    }

    public static PblOutboundPutBytesMessage createPut(UnsignedInteger cookie, UnsignedInteger payloadSize, byte[] payload) {
        PblOutboundPutBytesMessage message = new PblOutboundPutBytesMessage(PutBytesCommand.PUT, cookie);
        message.setSize(payloadSize);
        message.setPayload(payload);
        return message;
    }

    private void writePutBytes() {
        addBytes(Byte.valueOf(this.mCommand.getId()));
        addBytes(ByteUtils.unsignedInt2bytes(this.mCookie));
        addBytes(ByteUtils.unsignedInt2bytes(this.mSize));
        if (this.mSize.intValue() >= this.mPayload.length) {
            addBytes(this.mPayload);
        } else {
            addBytes(Arrays.copyOf(this.mPayload, this.mSize.intValue()));
        }
    }

    public static PblOutboundPutBytesMessage createCommit(UnsignedInteger cookie, UnsignedInteger crc) {
        PblOutboundPutBytesMessage message = new PblOutboundPutBytesMessage(PutBytesCommand.COMMIT, cookie);
        message.setCrc(crc);
        return message;
    }

    private void writeCommitBytes() {
        addBytes(Byte.valueOf(this.mCommand.getId()));
        addBytes(ByteUtils.unsignedInt2bytes(this.mCookie));
        addBytes(ByteUtils.unsignedInt2bytes(this.mCrc));
    }

    public static PblOutboundPutBytesMessage createAbort(UnsignedInteger cookie) {
        return new PblOutboundPutBytesMessage(PutBytesCommand.ABORT, cookie);
    }

    public static PblOutboundPutBytesMessage createInstall(UnsignedInteger cookie) {
        return new PblOutboundPutBytesMessage(PutBytesCommand.INSTALL, cookie);
    }

    private void writeAbortOrInstallBytes() {
        addBytes(Byte.valueOf(this.mCommand.getId()));
        addBytes(ByteUtils.unsignedInt2bytes(this.mCookie));
    }

    public synchronized byte[] getBytes() {
        if (!areBytesCached()) {
            switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundPutBytesMessage$PutBytesCommand[this.mCommand.ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    writeInitBytes();
                    break;
                case SMTPMessage.RETURN_HDRS /*2*/:
                    writePutBytes();
                    break;
                case ListInfo.INDETERMINATE /*3*/:
                    writeCommitBytes();
                    break;
                case SMTPMessage.NOTIFY_DELAY /*4*/:
                case R.styleable.WalletFragmentStyle_maskedWalletDetailsHeaderTextAppearance /*5*/:
                    writeAbortOrInstallBytes();
                    break;
            }
        }
        return super.getBytes();
    }
}
